using System;
using System.Configuration;
using System.Web;
using System.Xml;
using System.Xml.Xsl;
using System.Net.Mail;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-03-08	
	Status:		release	
	Version:	2.0.0
	Buidl:		20070524
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070524:	Modification for .Net 2.0
	20070703:	Removed net_2_0 directive (from compatibility build)
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu engineX blog page class.
	/// </summary>
	public class sitePageX : uiX {
		#region Private methods
		private bool _wantArchives;
		private const string _email = "customer@umlungu.com";
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public sitePageX() : base("sitePageX") {
		}
		/// <summary>Constructor</summary>
		public sitePageX(string thisname) : base(thisname) {
		}
		#endregion

		#region Protected methods
		/// <summary>
		/// Method description
		/// </summary>
		protected override void _LoadIt() {
			_LoadParameters();
			_LoadTemplate();
			BlogXml.LoadXml(String.Concat("<", Name, "/>"));
		}
		/// <summary>
		/// Loads the query string parameters
		/// </summary>
		private void loadParameters() {
			base._LoadParameters();
			_wantArchives = (BlogPage == "home" || BlogPage == "press");
			if (_wantArchives)
				Topic = "default";
		}
		/// <summary>
		/// Loads the blog xsl
		/// </summary>
		protected override void _LoadTemplate() {
			Space = new x_blogspace(Application);
			if (!_wantArchives) {
				Template = Space.GetTransform(Name);
				Space.Current(true);
				Space.RemoveArchives();
				Space.RemoveBlogs();
			} else {
				Space.Current(Name, Topic, false);
				Template = Space.GetTransform();
				Space.Current(true);
				Space.RemoveArchives();
				Space.RemoveRecent();
				Space.RemoveComment();
			}
			Space.RemoveUsers();
			//	Add xsl parameters.
			TemplateArgs = new XsltArgumentList();
			TemplateArgs.AddParam("name", "", Name);
			TemplateArgs.AddParam("page", "", BlogPage);
			TemplateArgs.AddParam("topic", "", Topic);
		}
		/// <summary>
		/// Loads the blog xml
		/// </summary>
		protected override void _LoadContent() {
			if (BlogPage == "response" )
				sendContact();
			if (BlogPage == "default" || BlogPage == "response" ) {
				if (Topic == "")
					_LoadAll();
				if (BlogID != null)
					_LoadBlog();
				else
					_LoadBlogList();
			}
			else if (BlogPage == "archive" )
				_LoadBlogArchives();
			else
				_LoadBlogspace();
			// load extra data in all cases.
			loadExtra();
		}
		/// <summary>
		/// Loads the blogspace xml
		/// </summary>
		protected override void _LoadBlogspace() {
			BlogXml.DocumentElement.InnerXml = Space.Blogspace.OuterXml;
		}
		/// <summary>
		/// Loads the blogspace archives xml
		/// </summary>
		protected override void _LoadBlogArchives() {
			Space.Archives(Name);
			BlogXml.DocumentElement.InnerXml = Space.Blogspace.OuterXml;
		}
		/// <summary>
		/// Loads a blog xml
		/// </summary>
		protected override void _LoadAll() {
			Space.Current(false);
			BlogXml.DocumentElement.InnerXml = Space.Blogspace.OuterXml;
		}
		/// <summary>
		/// Loads a blog xml
		/// </summary>
		protected override void _LoadBlog() {
			x_blog thisblog = new x_blog(Application);
			if (Archive == "")
				thisblog.Get(Name, Topic, BlogID);
			else
				thisblog.Get(Name, Topic, Archive, BlogID);
			BlogXml.DocumentElement.InnerXml = String.Concat(thisblog.Blog.OuterXml, Space.Blogspace.OuterXml);
		}
		/// <summary>
		/// Loads the blog xml
		/// </summary>
		protected override void _LoadBlogs() {
			x_blog thisblog = new x_blog(Application);
			if (Archive == "")
				thisblog.Gets(Name, Topic, BlogIDs, true, true);
			else
				thisblog.Gets(Name, Topic, Archive, BlogIDs, true, true);
			BlogXml.DocumentElement.InnerXml = String.Concat(thisblog.Blogs.OuterXml, Space.Blogspace.OuterXml);
		}
		/// <summary>
		/// Loads the blog xml
		/// </summary>
		protected override void _LoadBlogList () {
			x_blog thisblog = new x_blog(Application);
			thisblog.List(Name, Topic);
			BlogXml.DocumentElement.InnerXml = String.Concat(thisblog.BlogList.OuterXml, Space.Blogspace.OuterXml);
		}
		#endregion

		#region Private methods
		/// <summary>
		/// Loads and appends any extra data
		/// </summary>
		/// <remarks>
		/// All errors in loading the extra xml data (file existence or parseError) are caught.
		/// This means errors result in the page loading without the extra xml data.
		/// </remarks>
		private void loadExtra() {
			XmlDocument extraPage = new XmlDocument();
			try {
				extraPage.Load(String.Concat(Application["ExtraPathX"], Name, "/page.xml"));
				XmlNode data = extraPage.DocumentElement.SelectSingleNode(String.Concat("//", Name));
				if (data != null)
					BlogXml.DocumentElement.InnerXml = String.Concat(BlogXml.DocumentElement.InnerXml, data.InnerXml);
			} catch(System.Exception e) {
				if (Debug) {
					ErrorResult = new x_result("BlogX");
					x_exception xe = new x_exception("error_extra", e.Message);
					BlogXml.DocumentElement.InnerXml = String.Concat(BlogXml.DocumentElement.InnerXml, ErrorResult.AddError(xe).OuterXml);
				}
			}
		}
		/// <summary>
		/// Loads and appends any extra data
		/// </summary>
		/// <remarks>
		/// All errors in loading the extra xml data (file existence or parseError) are caught.
		/// This means errors result in the page loading without the extra xml data.
		/// </remarks>
		private void sendContact() {
			try {
				SmtpClient smtp = new SmtpClient();
				//smtp.Host = "localhost";
				smtp.Send("contact@umlungu.com", _email, "umlungu website contact", loadContact());
			}
			catch(System.Exception e) {
				Response.Write(String.Concat("\n<!-- Could not send email: '", e.Message, "' -->\n"));
			}
		}
		
		private string loadContact() {
			string name = (Request.Form["Contact_name"] == null)? "[unknown author]": Request.Form["Contact_name"];
			string email = (Request.Form["Contact_email"] == null)? "[no email address]": Request.Form["Contact_email"];
			string phone = (Request.Form["Contact_phone"] == null)? "[no phone number]": Request.Form["Contact_phone"];
			string text = (Request.Form["Contact_message"] == null)? "[no message]": Request.Form["Contact_message"];
			return(String.Concat("Name: ", name, "\nEmail: ", email, "\nPhone: ", phone, "\nText: ", text));
		}
		#endregion
	}
}
